<%@ page import="com.qinghe.pro.entity.main.Role"%>
<%@ page import="com.qinghe.pro.entity.main.Module"%>
<%@ page import="com.qinghe.pro.entity.main.Permission"%>
<%@ page import="com.qinghe.pro.entity.main.RolePermission"%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<!DOCTYPE html>
<html>
<head>
<title>设置权限</title>
<%@ include file="/WEB-INF/views/manager/include.inc.jsp"%>
<link rel="stylesheet" href="${contextPath }/styles/validationEngine/css/validationEngine.jquery.css">
<link href="${contextPath}/styles/treeTable/themes/default/treeTable.css" rel="stylesheet" type="text/css" />
<%!
public String role_tree(Module module, Role role, Integer[] permissionIndex) {
	StringBuilder explandBuilder = null;

	if (module.getParentId() != null) {
		explandBuilder = new StringBuilder("<tr id=\"" + module.getId() + "\" pId=\"" + module.getParentId() + "\">\n");
	} else {
		explandBuilder = new StringBuilder("<tr id=\"" + module.getId() + "\">\n");
	}

	explandBuilder.append("<td><input type='checkbox' name='check' onclick='selectall(this)' /><input type='checkbox' id='checkall[" + permissionIndex[0] + "]' name='checkall' onclick='selectallbox(this)' /><label for='checkall[" + permissionIndex[0] + "]'  style='cursor:pointer;'>" + module.getName() + "</label></td>\n");
	explandBuilder.append("<td>\n");

	for (Permission permission : module.getPermissions()) {
		explandBuilder.append("<label for='permissions[" + permissionIndex[0] + "].id'  style='cursor:pointer;'>" + permission.getName() + "</label><input type='checkbox' id='permissions[" + permissionIndex[0] + "].id' name='rolePermissions[" + permissionIndex[0] + "].permission.id' value='" + permission.getId() + "' onclick='checkallbox()' ");
		boolean isFind = false;
		for (RolePermission rolePermission : role.getRolePermissions()) {
			if (rolePermission.getPermission().getId().equals(permission.getId())) {
				explandBuilder.append("checked='checked'/>&nbsp;&nbsp;");
				explandBuilder.append("<input type='hidden' name='rolePermissions[" + permissionIndex[0] + "].id' value='" + rolePermission.getId() + "'/>");
				isFind = true;
				break;
			}
		}

		if (isFind == false) {
			explandBuilder.append("/>&nbsp;&nbsp;");
		}
		permissionIndex[0] += 1;
	}

	explandBuilder.append("</td>\n");
	explandBuilder.append("</tr>\n");

	for (Module o : module.getChildren()) {
		explandBuilder.append(role_tree(o, role, permissionIndex));
	}

	return explandBuilder.toString();
}
%>
<%
	Module module = (Module) request.getAttribute("module");
	Role role = (Role) request.getAttribute("role");
	String permissonTree = role_tree(module, role, new Integer[]{0});
%>
</head>
<body>
<!--top s-->
<c:import url="/public/header?tag=Config" />
<!--top end-->
<div class="m_main">
  <!--left s-->
  <c:import url="/public/left?tag=Config&tag1=Permission" />
  <!--left end-->
  <!--right s-->
  <div class="m_right fl">
	<form method="post" action="${contextPath}/manager/role/update" onsubmit="return validateCallback(this, callbackRef);">
	  <div class="r_box">
		<div class="title1"><h3>设置权限</h3></div>
		<div class="term">
		  <ul>
			<li>
			  <span>名称：</span>
			  <input name="name" type="text" class="inputx intb fl validate[required, maxSize[32]]" value="${role.name }" maxlength="32" />
			</li>
			<li>
			  <span>描述：</span>
			  <input name="description" type="text" class="inputx intb fl" value="${role.description }" maxlength="128" />
			</li>
		</ul>
		</div>
		<!-- <div class="title2">模块名称</div> -->
		<table id="treeTable" width="100%">
		  <tr height="50px">
			<td>模块名称</td>
			<td width="70%" style="border-left: none;">操作权限</td>
		  </tr>
		  <%=permissonTree%>
		</table>
		<input type="hidden" name="id" value="${role.id }" />
        <div class="tc">
          <input type="button" class="back_btn" value="返 回" onclick="history.go(-1)">
		  <input name="submit" type="submit" class="keep_btn" value="提交" />
        </div>
	  </div>
	</form>
  </div>
  <!--right end-->
</div>
<div class="foot"></div>
<script src="${contextPath }/styles/validationEngine/js/jquery.validationEngine.min.js"></script>
<script src="${contextPath }/styles/validationEngine/js/jquery.validationEngine-zh_CN.js"></script>
<script src="${contextPath }/styles/treeTable/jquery.treeTable.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
	var option = {
		theme : 'default',
		expandLevel : 4
	};
	$('#treeTable').treeTable(option);

	checkallbox();
});
//树的全选
function selectall(pCheckbox) {
	var id = $(pCheckbox).parent().parent().attr("id");
	var chk = $(pCheckbox).parent().parent().parent().find("tr[pid='" + id + "']");
	var children = chk.children().find(":checkbox[name='check']");
	$(pCheckbox).next().attr('checked', pCheckbox.checked).click().attr('checked', pCheckbox.checked);
	children.attr('checked', pCheckbox.checked).click().attr('checked', pCheckbox.checked);
}
//全选
function selectallbox(pCheckbox) {
	var ids = $(pCheckbox).parent().parent().find(":checkbox[name^='rolePermissions']");
	var it = ids.length;
	while (it--) {
		ids[it].checked = pCheckbox.checked;
	}
}
//验证全选
function checkallbox() {
	$("#treeTable tr").each(
		function() {
			var ck = $(this).children().find(":checkbox[name^='rolePermissions']");
			var tag = true;
			for (var i = 0; i < ck.length; i++) {
				if (!$(ck[i]).attr("checked")) {
					tag = false;
				}
			}
			if (tag) {
				$(this).children().find(":checkbox[name='checkall']").attr("checked", true);
			} else {
				$(this).children().find(":checkbox[name='checkall']").attr("checked", false);
			}
		}
	);
}
function close2refresh() {
	goToPage("${contextPath}/manager/role/list");
}
</script>
</body>
</html>